from flask_caching import Cache
from flask import current_app

from config import GlobalContextData

class FlaskTool:
    Cache = None

    @property
    def cache(self):
        if not FlaskTool.Cache:
            if not GlobalContextData.CacheDir.is_dir():
                GlobalContextData.CacheDir.mkdir(parents=True)
            cache = Cache(config={
                'CACHE_TYPE': 'FileSystemCache',
                'CACHE_DIR': str(GlobalContextData.CacheDir.absolute()),
                'CACHE_DEFAULT_TIMEOUT': 300  # 默认超时时间（秒）
            })
            cache.init_app(current_app)
            FlaskTool.Cache = cache
            print(f' * Set Cache Path: {GlobalContextData.CacheDir.absolute()}')

        return FlaskTool.Cache.cache

flask_tool = FlaskTool()
